package com.leTest.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leTest.eduService.entity.CourseDescriptionEntity;
import com.leTest.eduService.entity.CourseEntity;
import com.leTest.eduService.entity.frontVo.CourseFrontVo;
import com.leTest.eduService.entity.frontVo.CourseWebVo;
import com.leTest.eduService.entity.vo.CourseInfoVo;
import com.leTest.eduService.mapper.CourseMapper;
import com.leTest.eduService.service.CourseDescriptionService;
import com.leTest.eduService.service.CourseService;
import com.leTest.exceptionhandler.CustomExceptionHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author le
 * @since 2025-02-16
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseEntity> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        CourseEntity courseEntity = new CourseEntity();
        BeanUtils.copyProperties(courseInfoVo, courseEntity);
        int insert = baseMapper.insert(courseEntity);
        if (insert != 1) {
            throw new CustomExceptionHandler(100,"添加课程失败！");
        }

        CourseDescriptionEntity courseDescriptionEntity = new CourseDescriptionEntity();
        BeanUtils.copyProperties(courseInfoVo, courseDescriptionEntity);
        courseDescriptionEntity.setId(courseEntity.getId());
        courseDescriptionService.save(courseDescriptionEntity);

        return courseEntity.getId();
    }

    //根据课程id查询课程基本信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //1.查询课程表
        CourseEntity eduCourse = baseMapper.selectById(courseId);
        //2.将数据封装到CourseInfoVo对象中
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //3.查询课程简介表
        CourseDescriptionEntity courseDescription = courseDescriptionService.getById(courseId);
        //4.将数据封装到CourseInfoVo对象中
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        CourseEntity ccourseEntity = new CourseEntity();
        BeanUtils.copyProperties(courseInfoVo, ccourseEntity);
        int update = baseMapper.updateById(ccourseEntity);
        if (update != 1) {
            throw new CustomExceptionHandler(100, "变更课程失败");
        }

        CourseDescriptionEntity courseDescriptionEntity = new CourseDescriptionEntity();
        courseDescriptionEntity.setId(courseInfoVo.getId());
        courseDescriptionEntity.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(courseDescriptionEntity);

    }

    //课程条件查询带分页
    @Override
    public Map<String, Object> getCourseFrontList(Page<CourseEntity> pageParam, CourseFrontVo courseFrontVo) {
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        //判断条件值是否为空,不为空就拼接条件
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度排序
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新时间排序
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) { //价格排序
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);
        //获取分页所有数据
        List<CourseEntity> records = pageParam.getRecords(); //该页数据的list集合
        long current = pageParam.getCurrent(); //当前页
        long pages = pageParam.getPages(); //总页数
        long size = pageParam.getSize(); //每页记录数
        long total = pageParam.getTotal(); //总记录数
        boolean hasNext = pageParam.hasNext(); //是否有下一页
        boolean hasPrevious = pageParam.hasPrevious(); //是否有上一页

        //把分页数据放到map集合中
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //根据课程id查询课程信息(手写sql语句来实现)
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
